package com.star.mentor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.star.core.base.BaseController;
import com.star.mentor.dto.MentorPageDTO;
import com.star.mentor.dto.MentorSaveDTO;
import com.star.mentor.dto.MentorUpdateWxmentorDTO;
import com.star.mentor.service.MentorService;
import com.star.mentor.vo.MentorPageVO;
import com.star.security.annotations.Authorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 导师
 *
 * @author yangzj
 */
@RestController
@RequestMapping("/mentor")
public class MentorController extends BaseController {

    @Autowired
    private MentorService mentorService;

    /**
     * 获取邀请码
     *
     * @return {@link String}
     */
    @Authorize("mentor:invitation")
    @GetMapping("/invitation-code")
    public String getInvitationCode(){
        return mentorService.getInvitationCode();
    }

    /**
     * 保存（创建邀请码）
     *
     * @param dto 入参
     */
    @Authorize("mentor:invitation")
    @PostMapping("/invitation-code")
    public void save(@Validated @RequestBody MentorSaveDTO dto){
        mentorService.save(dto);
    }

    /**
     * 更新启用
     *
     * @param id      ID
     * @param enabled 启用
     */
    @Authorize("mentor:enabled")
    @PutMapping("/enabled/{id}/{enabled}")
    public void updateEnabled(@PathVariable("id") Long id, @PathVariable("enabled") boolean enabled) {
        mentorService.updateEnabled(id, enabled);
    }

    /**
     * 延长邀请码有效时间
     *
     * @param invitationExpire 邀请过期
     * @param id               ID
     */
    @Authorize("mentor:invitation")
    @PutMapping("/invitation-code/{id}/{invitationExpire}")
    public void updateInvitationExpire(@PathVariable("id") Long id, @PathVariable("invitationExpire")  Integer invitationExpire){
        mentorService.updateInvitationExpire(id, invitationExpire);
    }

    /**
     * 删除通过ID
     *
     * @param id ID
     */
    @Authorize("mentor:delete")
    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable("id") Long id){
        mentorService.deleteById(id);
    }

    /**
     * 分页
     *
     * @param dto 入参
     * @return {@link IPage}<{@link MentorPageVO}>
     */
    @Authorize("mentor:page")
    @PostMapping("/page")
    public IPage<MentorPageVO> page(@Validated @RequestBody MentorPageDTO dto){
        return mentorService.page(dto);
    }

    /**
     * 验证邀请码-微信小程序（导师端）
     *
     * @param invitationCode 邀请代码
     * @return {@link Long} 导师id
     */
    @Authorize("wxmentor")
    @GetMapping("/wxmentor/validate/{invitationCode}")
    public Long validateWxmentor(@PathVariable("invitationCode") String invitationCode){
        return mentorService.validateWxmentor(invitationCode);
    }

    /**
     * 更新-微信小程序（导师端）
     *
     * @param dto    入参
     */
    @Authorize("wxmentor")
    @PutMapping("/wxmentor")
    public void updateWxmentor(@Validated @RequestBody MentorUpdateWxmentorDTO dto){
        mentorService.updateWxmentor(dto, getUserId());
    }
}
